home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / new2q.src < prev    next >
Text File  |  1991-05-29  |  2KB  |  64 lines

  1. %%HP:T(3)F(.);
  2. @ by the HP 48 Design Team
  3. @
  4. @ NEW2Q: Version of ->Q based on J.K.Horn's Algorithm [see DEC2FRAC on this
  5. @ disk...  -jkh-], but uses exit conditions like those of ->Q.
  6. @
  7. @ Input:
  8. @
  9. @ 2: Decimal Number to be converted to a fraction
  10. @ 1: Number of decimal places of zeros required in the error.
  11. @
  12. @ Output:
  13. @
  14. @ 1: 'Numerator/Denominator'
  15. @
  16. @ Example:
  17. @
  18. @ What's the simplest fraction which agrees with sqrt(3) to 4 decimal places?
  19. @   3 √ 4 NEQ2Q returns '97/56'
  20. @   '97/56-√3' EVAL returns .00009294957
  21. @                                 ^^^^  note 4 zeros.
  22. @
  23. @
  24.  
  25. \<< -3 CF DUP2
  26.   IF 1 > SWAP FP AND
  27.   THEN OVER XPON 1 -                        @ calculate the input exponent.
  28.     \<< \-> X 'IFTE(X==0,-500,XPON(X))' \>> @ define a 0-tolerant XPON.
  29.   \-> f c x expo
  30.     \<< 0 1 1 f DUP IP SWAP FP              @ set recursion initial cond.s.
  31.       WHILE
  32.        OVER 5 PICK / f - ABS expo EVAL      @ calculate expon. of error
  33.        x SWAP - c <                         @ and compare with input.
  34.        OVER AND                             @ if not close enough and
  35.                                             @ the remainder's non-zero
  36.       REPEAT
  37.        INV DUP FP SWAP IP                   @ then calculate next iterate.
  38.        \-> B0 B1 A0 A1 R B
  39.         \<< B1 'B*B1+B0' EVAL
  40.             A1 'B*A1+A0' EVAL
  41.             R
  42.         \>>
  43.       END
  44.       DROP SWAP DROP SWAP
  45.       DUP 4 ROLL - DUP f * 0 RND            @ calc. "missing" den. and num.
  46.       \-> N D D0 N0
  47.       \<<
  48.         IF 'x-expo(ABS(f-N0/D0))<c'         @ if "missing" frac. is not
  49.         THEN N D                            @ good enough, use original.
  50.         ELSE N0 D0
  51.           IF 'N0\=/N'                       @ If it is really new,
  52.           THEN 200 .2 BEEP                  @ then beep.
  53.           END
  54.         END
  55.       \>>
  56.     \>>                                     @ We're done, now clean up.
  57.     IF DUP ABS 1 >
  58.     THEN # 352318d SYSEVAL
  59.     ELSE DROP
  60.     END
  61.   ELSE DROP
  62.   END
  63. \>>
  64.